package com.taobao.top
{
	import com.adobe.serialization.json.JSON;
	import com.taobao.top.model.LogisticCompany;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	/**
	 *  
	 * @author Administrator
	 * 
	 */	
	public class TOPLogisticCompanies extends EventDispatcher
	{
		public function TOPLogisticCompanies()
		{
		}
		
		/**
		 * 
		 * @param fields 需返回的字段列表。可选值:LogisticCompany 结构中的所有字段;多个字段间用","逗号隔开. 如:company_id,company_code,company_name。可以com.taobao.top.enum.LogisticCompanyFields枚举
		 * @param is_recommended 是否查询推荐物流公司.可选值:true,false.如果不提供此参数,将会返回所有支持电话联系的物流公司. 
		 * @param orderMode 推荐物流公司的下单方式.可选值:offline(电话联系/自己联系),online(在线下单),all(即电话联系又在线下单). 此参数仅仅用于is_recommended 为ture时。就是说对于推荐物流公司才可用.如果不选择此参数将会返回推荐物流中支持电话联系的物流公司.可以com.taobao.top.enum.LogisticCompanyOrderMode枚举 
		 * 
		 */		
		
		public function getLogisticCompanies(fields:Array,is_recommended:Boolean = false,orderMode:String = "all"):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.logisticcompanies.get"),
				new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fields)),
				new TopRequestParam("is_recommended",is_recommended.toString()),
				new TopRequestParam("orderMode",orderMode)];
			topURLLdr.addEventListener(Event.COMPLETE,logisticCompaniesGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function logisticCompaniesGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var areasArr:Array = logisticCompaniesGetJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function logisticCompaniesGetJsonParser(jsonStr:String):Array
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				return objToLogisticCompaniesArray((JSON.decode(jsonStr) as Object).rsp);
			}
			return null;
		}
		
		private function objToLogisticCompaniesArray(obj:Object):Array
		{
			var logComArr:Array = new Array();
			for each(var logObj:Object in obj.logistic_companies)
			{
				var logCom:LogisticCompany = new LogisticCompany();
				logCom.company_code = obj.company_code;
				logCom.company_id = obj.company_id;
				logCom.company_name = obj.company_name;
				logComArr.push(logCom);
			}
			return logComArr;
		}
	}
}